(function($) {
	$.alert = function(message, timeToLive, options) {

		var ttl = timeToLive/1000;

		var opt = $.extend( {
			title : window.location.host,
			backgroudColor: "#ECECEC",//背景色
            backgroundImage: "/images/loading.gif",//背景图片
            width: 248,//宽度
            height: 100,//高度
			modal : false,
			type:0 //0全部遮，1 局部遮
		}, options || {});

        if (opt.type == 0) {
            //全屏遮
            openLayer();
        } else {
            //局部遮(当前对象应为需要被遮挡的对象)
            openPartialLayer(this);
        }
         
        //销毁对象
        if (opt.option === "destroy") {
            closeLayer();
        }
	
                 
        //设置背景层高
        function height () {
            var scrollHeight, offsetHeight;
            // handle IE 6
            if ($.support.boxModel && $.support.leadingWhitespace) {
                scrollHeight = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight);
                offsetHeight = Math.max(document.documentElement.offsetHeight, document.body.offsetHeight);
                if (scrollHeight < offsetHeight) {
                    return $(window).height();
                } else {
                    return scrollHeight;
                }
                // handle "good" browsers
            }
            else if ($.support.objectAll) {
                return $(document).height() - 4;
            }
            else {
                return $(document).height()+500;
            }
        };
         
        //设置背景层宽
        function width() {
            var scrollWidth, offsetWidth;
            // handle IE
            if ($.support.boxModel) {
                scrollWidth = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth);
                offsetWidth = Math.max(document.documentElement.offsetWidth, document.body.offsetWidth);
                if (scrollWidth < offsetWidth) {
                    return $(window).width();
                } else {
                    return scrollWidth;
                }
                // handle "good" browsers
            }
            else {
                return $(document).width();
            }
        };
        
                /*==========全部遮罩=========*/
        function openLayer() {
            //背景遮罩层
            var layer = $("<div id='layer'></div>");
            layer.css({
                zIndex:9998,
                position: "absolute",
                height: height() + "px",
                width: width() + "px",
                background: "black",
                top: 0,
                left: 0,
                filter: "alpha(opacity=30)",
                opacity: 0.3
               
            });
            
           //图片及文字层
            var content = $("<div id='content'></div>");
            content.css({
                textAlign: "left",
                position:"absolute",
                zIndex: 9999,
                height: opt.height + "px",
                width: opt.width + "px",
                top: "30%",
                left: "50%",
                verticalAlign: "middle",
                background: opt.backgroudColor,
                borderRadius:"8px",
                fontSize:"13px"
            });
             content.append('<div class="panel panel-default">'+
			   '<div class="panel-heading">'+
			      '<p class="text-info">消息提示:</p>' +
			   '</div>'+
			   '<div class="panel-body">'+
			    '<p class="text-warning">'+message+'</p>'+
			     '<br><br><img style="vertical-align:middle;margin:'+(opt.height/10)+'px; 0 0 5px;margin-right:5px;width:20px;height:20px;" src="' + opt.backgroundImage + '" />'+
			         '提示信息将在<span id="spanId">'+ttl+'</span>秒后<button id="btnClose" type="button" class="btn btn-default btn-xs">'+
                       '<span class="glyphicon glyphicon-off"></span>关闭'+
					   '</button>.'+
			   '</div>'+
			'</div>');
            //content.append("<img style='vertical-align:middle;margin:"+(opt.height/4)+"px; 0 0 5px;margin-right:5px;' src='" + opt.backgroundImage + "' /><span style='text-align:center; vertical-align:middle;'><span id='spanId'>"+ttl+"</span>" + message + "</span>");
            $("body").append(layer).append(content);
            var top = content.css("top").replace('px','');
            var left = content.css("left").replace('px','');
            content.css("top",(parseFloat(top)-opt.height/2)).css("left",(parseFloat(left)-opt.width/2));
             
           return this;
        }
                 
        /*==========局部遮罩=========*/
        function openPartialLayer(obj) {
          
            var eheight = $(obj).css("height");//元素带px的高宽度
            var ewidth = $(obj).css("width");
            var top = $(obj).offset().top; // 元素在文档中位置 滚动条不影响
            var left = $(obj).offset().left;
            var layer = $("<div id='partialLayer'></div>");
            layer.css({
                style: 'z-index:9998',
                position: "absolute",
                height: eheight,
                width: ewidth,
                background: "black",
                top: top,
                left: left,
                filter: "alpha(opacity=60)",
                opacity: 0.6,
                borderRadius:"3px",
                dispaly: "block"
            });
            //图片及文字层
            var reg = /\d+/;
            var num = eheight.match(reg)[0];
            var content = $("<div id='content'></div>");
            content.css({
                textAlign: "left",
                position:"absolute",
                zIndex: 9999,
                height: opt.height + "px",
                width: opt.width + "px",
                top: (num/2-20)+"px",
                left: "40%",
                verticalAlign: "middle",
                background: opt.backgroudColor,
                borderRadius:"8px",
                fontSize:"13px"
            });
            content.append("<img style='vertical-align:middle;margin:"+(opt.height/4)+"px; 0 0 5px;margin-right:5px;' src='" + opt.backgroundImage + "' /><span style='text-align:center; vertical-align:middle;'><span id='spanId'>"+ttl+"</span>" + message + "</span>");

            $("body").append(layer).append(content);
            return this;
        }
        //销毁对象
        function closeLayer() {
            $("#layer,#content,#partialLayer").remove();
            return this;
        };
        
        function timeCompute(){
        	--ttl;
        	if(ttl > 0){
        		//每1S执行一次
        		$("#spanId").text(ttl);
        		setTimeout(function() {timeCompute();}, 1000);
        	}else{
        		closeLayer();
        	}
        }        
        
        $("#btnClose").click(function(){
        	ttl = 0;
        	closeLayer();
        });
        
		setTimeout(function() {
			timeCompute();
		}, 1000);
	}
})(jQuery);